<?php
$uid = session('user_id');
include_once(ROOT_PATH . 'includes/cls_image.php');
$image = new cls_image($_CFG['bgcolor']);
$type = 'app';
if (!is_dir(ROOT_PATH.'/data/app/'))
{
    mkdir(ROOT_PATH.'/data/app/');
}
($avatar = $image->upload_image($_FILES['avatary'], $type)) || abort($image->error_msg());

$allow_avatar_type = ['jpg', 'jpeg', 'png'];

@list($file_name, $file_ext) = explode('.', basename($avatar));
in_array($file_ext, $allow_avatar_type) || abort(33);
$new_name = EC_PATH.DS.get_user_path(session('user_id')).DS.$uid.'.';
$old_path = EC_PATH.DS.$avatar;
is_file($old_path) || abort(34);
// 删除原图
foreach ($allow_avatar_type as $v)
{
    is_file($new_name.$v) && unlink($new_name.$v);
}
$new_path = $new_name.$file_ext;
rename($old_path, $new_path) || abort(35);
$_SESSION['user']['avatar'] = get_avatar($uid).'?'.time();
return ['avatar'=>get_avatar($uid).'?'.time()];